//
//  CompanyDisplayViewController.m
//  陶瓷
//
//  Created by 王戈 on 15/7/12.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "CompanyDisplayViewController.h"
#import "TailDetailTableViewCell.h"
#import "MineLinkUsViewController.h"

static const CGFloat KJianJu = 12;


@interface CompanyDisplayViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *companyImageView;
//@property (nonatomic, strong) UILabel *detailLabel;

@property (nonatomic, strong) UILabel *brandTitle;
@property (nonatomic, strong) UILabel *brandDetail;
@property (nonatomic, strong) UILabel *nameTitle;
@property (nonatomic, strong) UILabel *nameDetail;
@property (nonatomic, strong) UILabel *personTitle;
@property (nonatomic, strong) UILabel *personDetail;
@property (nonatomic, strong) UILabel *numTitle;
@property (nonatomic, strong) UILabel *numDetail;
@property (nonatomic, strong) UILabel *addressTitle;
@property (nonatomic, strong) UILabel *addressDetail;

@property (nonatomic, strong) UIButton *weizhanBtn;
@property (nonatomic, strong) UIButton *hereBtn;
@property (nonatomic, strong) UIButton *phoneBtn;
@property (nonatomic, strong) UIButton *gethereBtn;
@property (nonatomic, strong) UIView *whiteLine;

//@property (nonatomic, strong) UIView *mapView;
@property (nonatomic, strong) BMKMapView *mapView;
@property (nonatomic, strong) BMKLocationService *locService;
@property (nonatomic, assign) CGFloat locaLatitude;
@property (nonatomic, assign) CGFloat locaLongitude;

@property (nonatomic, strong) BMKGeoCodeSearch *searcher;

@property (nonatomic, strong) UIView *grayLine;
@property (nonatomic, strong) UIButton *messageLabel;

@end

@implementation CompanyDisplayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupChildViews];
//    self.title = self.company.Brand;
    self.title = @"企业信息";
    
    _searcher =[[BMKGeoCodeSearch alloc]init];
    _searcher.delegate = self;
    BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
    geoCodeSearchOption.city= [TCBaseTool sharedBaseTool].currentCityName;
    geoCodeSearchOption.address = self.company.Address;
    BOOL flag = [_searcher geoCode:geoCodeSearchOption];
    if(flag)
    {
        NSLog(@"geo检索发送成功");
    }
    else
    {
        NSLog(@"geo检索发送失败");
    }
    
}

#pragma mark - BMKGeoCodeSearchDelegate
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
        BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
        CLLocationCoordinate2D coor;
        coor.latitude = result.location.latitude; //[self.company.Latitude floatValue];
        coor.longitude = result.location.longitude;//[self.company.Longitude floatValue];
        annotation.coordinate = coor;
        annotation.title = self.company.Brand;
        [_mapView addAnnotation:annotation];
        [_mapView setCenterCoordinate:coor];
        _mapView.zoomLevel = 16;
        [self mapDisable];
    }
    else {
        NSLog(@"抱歉，未找到结果");
    }
}


- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.navigationItem setTitle:@""];
}

- (void)setupChildViews {
    self.scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.scrollView];
    
    self.companyImageView = [[UIImageView alloc] init];
    [_companyImageView sd_setImageWithURL:[NSURL URLWithString:_company.HeadImageUrl]];
    [self.scrollView addSubview:_companyImageView];
    self.companyImageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *topGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoWebVC:)];
    topGesture.numberOfTapsRequired = 1;
    [self.companyImageView addGestureRecognizer:topGesture];
    
    //    self.detailScrollLabel = [[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_companyImageView.frame) + 10, SCREEN_WIDTH, 260)];
    //    [self.view addSubview:self.detailScrollLabel];
    
    self.grayLine = [[UIView alloc] init];
    self.grayLine.backgroundColor = TCColor(243, 243, 243, 1);
    [self.scrollView addSubview:self.grayLine];
    
    self.messageLabel = [[UIButton alloc] init];
    self.messageLabel.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.messageLabel setTitle:@"信息" forState:UIControlStateNormal];
    [self.messageLabel setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.scrollView addSubview:self.messageLabel];
    
    _brandTitle = [self titleLabelWithType:YES];
    _brandDetail = [self titleLabelWithType:NO];
    _nameTitle = [self titleLabelWithType:YES];
    _nameDetail = [self titleLabelWithType:NO];
    _personTitle = [self titleLabelWithType:YES];
    _personDetail = [self titleLabelWithType:NO];
    _numTitle = [self titleLabelWithType:YES];
    _numDetail = [self titleLabelWithType:NO];
    _addressTitle = [self titleLabelWithType:YES];
    _addressDetail = [self titleLabelWithType:NO];
    
    [_scrollView addSubview:_brandTitle];
    [_scrollView addSubview:_brandDetail];
    [_scrollView addSubview:_nameTitle];
    [_scrollView addSubview:_nameDetail];
    [_scrollView addSubview:_personDetail];
    [_scrollView addSubview:_personTitle];
    [_scrollView addSubview:_numDetail];
    [_scrollView addSubview:_numTitle];
    [_scrollView addSubview:_addressDetail];
    [_scrollView addSubview:_addressTitle];
    
    UIButton *weizhanBtn = [[UIButton alloc] init];
    [weizhanBtn setTitle:@"开通企业微站" forState:UIControlStateNormal];
    [weizhanBtn.titleLabel setFont:_brandTitle.font];
    [weizhanBtn setTitleColor:TCMainBGColor forState:UIControlStateNormal];
    [weizhanBtn addTarget:self action:@selector(weizhanBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.weizhanBtn = weizhanBtn;
    [self.scrollView addSubview:weizhanBtn];
    
    UIButton *hereBtn = [[UIButton alloc] init];
    [hereBtn setTitle:@"" forState:UIControlStateNormal];
    hereBtn.titleLabel.font = _weizhanBtn.font;
    [hereBtn setTitleColor:TCMainBGColor forState:UIControlStateNormal];
    self.hereBtn = hereBtn;
    [self.scrollView addSubview:hereBtn];
    
    UIButton *phoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [phoneBtn setTitle:@"给企业致电" forState:UIControlStateNormal];
    phoneBtn.backgroundColor = TCMainBGColor;
    phoneBtn.titleLabel.textColor = [UIColor whiteColor];
    phoneBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [phoneBtn addTarget:self action:@selector(clickTel:) forControlEvents:UIControlEventTouchUpInside];
    self.phoneBtn = phoneBtn;
    [self.view addSubview:phoneBtn];
    
    UIView *whiteLine = [[UIView alloc] init];
    whiteLine.backgroundColor = [UIColor whiteColor];
    self.whiteLine = whiteLine;
    [self.view addSubview:whiteLine];
    
    UIButton *gethereBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [gethereBtn setTitle:@"到这里去" forState:UIControlStateNormal];
    gethereBtn.backgroundColor = TCMainBGColor;
    gethereBtn.titleLabel.textColor = [UIColor whiteColor];
    gethereBtn.titleLabel.font = phoneBtn.titleLabel.font;
    [gethereBtn addTarget:self action:@selector(clickFindCompany:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:gethereBtn];
    _gethereBtn = gethereBtn;
    
    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, kDefaultWindowWidth, 80)];
    [_mapView setMapType:BMKMapTypeStandard];
    
    [self.scrollView addSubview:_mapView];
    
    
}

- (void)gotoWebVC:(UITapGestureRecognizer *)gesture
{
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:self.company.MicroWeb]];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [_mapView viewWillAppear];
    _mapView.delegate = self;
    [self.navigationController setNavigationBarHidden:NO];
    [self setupLayout];
}

- (void)viewDidAppear:(BOOL)animated
{
    
}

- (void)mapDisable
{
    _mapView.zoomEnabled = NO;
    _mapView.zoomEnabledWithTap = NO;
    _mapView.scrollEnabled = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil;
    _searcher.delegate = nil;
}

- (void)weizhanBtn:(UIButton *)btn
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"如需开通企业微站请到个人中心联系客服！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alertView show];
}

#pragma mark - BMKMapViewDelegate
// Override
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"TCCompanyAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        return newAnnotationView;
    }
    return nil;
}

- (void)clickFindCompany:(id)sender
{
    //    TailMapViewController *mapvc = [[TailMapViewController alloc] init];
    //    [self.navigationController pushViewController:mapvc animated:YES];
    
    [self initBMKUserLocation];
}

- (void)clickTel:(id)sender
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",self.company.ContactNumber]]];
}


#pragma mark - BMKLocationServiceDelegate
/**
 *  初始化百度地图用户位置管理类
 */
- (void)initBMKUserLocation
{
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    [self startLocation];
}

/**
 *  打开定位服务
 */
-(void)startLocation
{
    [_locService startUserLocationService];
}

/**
 *  关闭定位服务
 */
-(void)stopLocation
{
    [_locService stopUserLocationService];
}

/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    self.locaLatitude = userLocation.location.coordinate.latitude;
    self.locaLongitude = userLocation.location.coordinate.longitude;
    [self stopLocation];//(如果需要实时定位不用停止定位服务)
    [self mapViewWillStartLocatingUser:nil];
}

-(void)mapViewWillStartLocatingUser:(BMKMapView *)mapView{
    NSLog(@"开始定位");
    BMKOpenTransitRouteOption *opt = [[BMKOpenTransitRouteOption alloc] init];
    opt.appScheme = @"baidumapsdk://mapsdk.baidu.com";//用于调起成功后，返回原应用
    //初始化起点节点
    BMKPlanNode* start = [[BMKPlanNode alloc]init];
    //指定起点经纬度
    CLLocationCoordinate2D coor1;
    coor1.latitude = self.locaLatitude;
    coor1.longitude = self.locaLongitude;
    //指定起点名称
    start.name = @"我的位置";
    start.pt = coor1;
    //指定起点
    opt.startPoint = start;
    
    //初始化终点节点
    BMKPlanNode* end = [[BMKPlanNode alloc]init];
    CLLocationCoordinate2D coor2;
    coor2.latitude = [self.company.Latitude floatValue];
    coor2.longitude = [self.company.Longitude floatValue];
    end.pt = coor2;
    //指定终点名称
    end.name = self.company.Address;
    opt.endPoint = end;
    
    //打开地图公交路线检索
    BMKOpenErrorCode code = [BMKOpenRoute openBaiduMapTransitRoute:opt];
}


- (void)setupLayout {
    _companyImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH*16/27);
    _grayLine.frame = CGRectMake(0, CGRectGetMaxY(_companyImageView.frame), SCREEN_WIDTH, 5);
    
    [_messageLabel sizeToFit];
    _messageLabel.y =CGRectGetMaxY(_grayLine.frame);
    _messageLabel.x = 10;
    
    [_weizhanBtn sizeToFit];
    _weizhanBtn.x = SCREEN_WIDTH - _weizhanBtn.width - 10 ;
    _weizhanBtn.y = _messageLabel.y;
    
    _brandTitle.text = @"公司品牌 :  ";
    _brandTitle.x = 15;
    _brandTitle.y = CGRectGetMaxY(_weizhanBtn.frame) + 20;
    [_brandTitle sizeToFit];
    
    _brandDetail.text = _company.Brand;
    CGFloat brandDetailX = CGRectGetMaxX(_brandTitle.frame);
    CGFloat brandDetailY = _brandTitle.y;
    CGFloat detailWidth = self.view.width - 30 - CGRectGetMaxX(_brandTitle.frame);
    CGFloat brandDetailHeight = [_brandDetail.text sizeWithfont:_brandTitle.font maxSize:CGSizeMake(detailWidth, CGFLOAT_MAX)].height;
    _brandDetail.frame = CGRectMake(brandDetailX, brandDetailY, detailWidth, brandDetailHeight);
    
    _nameTitle.text = @"公司名称 :  ";
    _nameTitle.x = _brandTitle.x;
    _nameTitle.y = CGRectGetMaxY(_brandDetail.frame) + KJianJu;
    [_nameTitle sizeToFit];
    
    _nameDetail.text = _company.Name;
    CGFloat nameDetailX = _brandDetail.x;
    CGFloat nameDetailY = _nameTitle.y;
    CGFloat nameDetailWidth = detailWidth;
    CGFloat nameDetailHeight = [_nameDetail.text sizeWithfont:_brandTitle.font maxSize:CGSizeMake(detailWidth, CGFLOAT_MAX)].height;
    _nameDetail.frame = CGRectMake(nameDetailX, nameDetailY, nameDetailWidth, nameDetailHeight);
    
    _personTitle.text = @"联 系 人  : ";
    _personTitle.x = _brandTitle.x;
    _personTitle.y = CGRectGetMaxY(_nameDetail.frame) + KJianJu;
    [_personTitle sizeToFit];
    
    _personDetail.text = _company.LinkMan;
    CGFloat personDetailX = _brandDetail.x;
    CGFloat personDetailY = _personTitle.y;
    CGFloat personDetailWidth = detailWidth;
    CGFloat personDetailHeight = [_personDetail.text sizeWithfont:_brandTitle.font maxSize:CGSizeMake(detailWidth, CGFLOAT_MAX)].height;
    _personDetail.frame = CGRectMake(personDetailX, personDetailY, personDetailWidth, personDetailHeight);
    
    _numTitle.text = @"联系电话 : ";
    _numTitle.x = _brandTitle.x;
    _numTitle.y = CGRectGetMaxY(_personTitle.frame) + KJianJu;
    [_numTitle sizeToFit];
    
    _numDetail.text = _company.ContactNumber;
    CGFloat numDetailX = _brandDetail.x;
    CGFloat numDetailY = _numTitle.y;
    CGFloat numDetailWidth = detailWidth;
    CGFloat numDetailHeight = [_numDetail.text sizeWithfont:_brandTitle.font maxSize:CGSizeMake(detailWidth, CGFLOAT_MAX)].height;
    _numDetail.frame = CGRectMake(numDetailX, numDetailY, numDetailWidth, numDetailHeight);
    
    _addressTitle.text = @"公司地址 : ";
    _addressTitle.x = _brandTitle.x;
    _addressTitle.y = CGRectGetMaxY(_numTitle.frame) + KJianJu;
    [_addressTitle sizeToFit];
    
    _addressDetail.text = _company.Address;
    CGFloat addressDetailX = _brandDetail.x;
    CGFloat addressDetailY = _addressTitle.y;
    CGFloat addressDetailWidth = detailWidth;
    CGFloat addressDetailHeight = [_addressDetail.text sizeWithfont:_brandTitle.font maxSize:CGSizeMake(detailWidth, CGFLOAT_MAX)].height;
    _addressDetail.frame = CGRectMake(addressDetailX, addressDetailY, addressDetailWidth, addressDetailHeight);
    
    [_hereBtn sizeToFit];
    _hereBtn.x = self.view.width - _hereBtn.width - 15;
    _hereBtn.y = CGRectGetMaxY(_addressDetail.frame);
    
    CGFloat mapViewY =CGRectGetMaxY(_hereBtn.frame);
    _mapView.frame = CGRectMake(0, mapViewY, SCREEN_WIDTH, 150);
    
    _scrollView.frame = self.view.bounds;
    _scrollView.height -= 40;
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, CGRectGetMaxY(_mapView.frame));
    
    CGFloat phoneBtnY = SCREEN_HEIGHT - 40;
    _phoneBtn.frame = CGRectMake(0, phoneBtnY, SCREEN_WIDTH * 0.5, 40);
    _whiteLine.frame = CGRectMake(CGRectGetMaxX(_phoneBtn.frame), _phoneBtn.y, 1, _phoneBtn.height);
    
    _gethereBtn.frame = _phoneBtn.frame;
    _gethereBtn.x = CGRectGetMaxX(_whiteLine.frame);
}

// 1为标题 ,0为内容
- (UILabel *)titleLabelWithType:(BOOL)type {
    UILabel *label = [[UILabel alloc] init];
    label.textColor = type ? [UIColor grayColor] : [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:16];
    label.numberOfLines = 0;
    //    label.backgroundColor = type ? [UIColor redColor] : [UIColor blueColor];
    return label;
}


/*
 NSString *temDetailString = [NSString stringWithFormat:
 @"公司品牌 :  %@\n公司名称 :  %@\n联  系  人 :  %@\n联系电话 :  %@\n",_company.Brand, _company.Name, _company.LinkMan, _company.ContactNumber];
 
 NSMutableAttributedString *detailString = [[NSMutableAttributedString alloc] initWithString:temDetailString];
 [detailString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:[temDetailString rangeOfString:@"公司品牌 :"]];
 [detailString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:[temDetailString rangeOfString:@"公司名称 :"]];
 [detailString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:[temDetailString rangeOfString:@"联  系  人 :"]];
 [detailString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:[temDetailString rangeOfString:@"联系电话 : "]];
 _detailLabel.attributedText = detailString;
 
 CGFloat labelX = 15;
 CGFloat labelY = 10 + CGRectGetMaxY(_weizhanBtn.frame) + 10;
 CGFloat labelW = SCREEN_WIDTH - 30;
 CGFloat labelH = [temDetailString sizeWithfont:_detailLabel.font maxSize:CGSizeMake(labelW, CGFLOAT_MAX)].height;
 _detailLabel.frame = CGRectMake(labelX, labelY, labelW, labelH);
 
 _addressTitle.text = @"公司地址 :  ";
 //    CGFloat addressTitleX = 15;
 //    CGFloat addressTitleY = CGRectGetMaxY(_detailLabel.frame);
 //    CGSize addressTitleSize = [@"公司地址 :" sizeWithfont:_detailLabel.font maxSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
 _addressTitle.x = 7;
 _addressTitle.y = CGRectGetMaxY(_detailLabel.frame);
 [_addressTitle sizeToFit];
 
 
 _addressDetail.text = _company.Address;
 CGFloat addressDetailX = CGRectGetMaxX(_addressTitle.frame);
 CGFloat addressDetailY = _addressTitle.y;
 CGFloat addressDetailWidth = self.view.width - 30 - CGRectGetMaxX(_addressTitle.frame);
 CGFloat addressDetailHeight = [_addressDetail.text sizeWithfont:_detailLabel.font maxSize:CGSizeMake(addressDetailWidth, CGFLOAT_MAX)].height;
 _addressDetail.frame = CGRectMake(addressDetailX, addressDetailY, addressDetailWidth, addressDetailHeight);
 
 
 */

@end
